<?php
class AdminAction extends Action
{

	public function _initialize()
	{
			$aid =Session::get('aid');
			if($aid != '1')
			{
					echo ' no power  to action this function';
					exit;
			}
			
	}

 function index()
 { 
  
  $this->display();
 }



	function returnugid($id) {
			if(!empty($id))
			{
					//
					 $gg = D('ybkguestgoods');
					 $arrs = $gg->field('goodsid')->where('`linkgid`='.$id.' and `isshow`=1')->select();
					 $good = D('goods');
					 $map['goods_id']=array('in',datasetToArray($arrs,'goodsid'));
					 $result = $good->field('goods_id,goods_name')->where($map)->select();
					 Return $result;
			}
	}

 function copyimg() {
	 $info ="";
		//这里获取用户的商品的ID
		$gid = intval($_GET['gid']);
		$idarr=datasetToArray($this->returnugid($gid),'goods_id');
		//dump($idarr); exit;
		if(count($idarr))
	 {
		$d = D('goods');
		$map['goods_id'] = array('in',$idarr);
		//dump($map);
		$result = $d->field('goods_img') ->where($map)->select();
		//dump($result); exit;
		$oldpath = C('OLDPATH');
		//TODO:taobao images
		$tbpath='userimgs/'.$gid.'/ybkgoods/';
		$pppath = 'userimgs/'.$gid.'/ybkppimg/';
		$yapath = 'userimgs/'.$gid.'/ybkyaimg/';
		createFolder($tbpath);
		createFolder($pppath);
		createFolder($yapath);
		//copy tabaoimg
		foreach($result as $value)
		{
				$file=$oldpath.$value['goods_img'];
				$tbnewfile = $tbpath.msubstr($value['goods_img'],24,25).'.tbi';
				if(!file_exists($tbnewfile))
				{
					copy($file,$tbnewfile);
				}
				
				$ppnewfile = $pppath.msubstr($value['goods_img'],24,40);
				if(!file_exists($ppnewfile))
				{
					copy($file,$ppnewfile);
				}
				
				$yanewfile = $yapath.msubstr($value['goods_img'],34,9).'.jpg';
				if(!file_exists($yanewfile))
				{
					copy($file,$yanewfile);
				}
				
		}
		$info .="copy ".count($result)." images ok!";
		//zip the document
		import('ORG.Io.PHPZip');
		$z = new PHPZip(); 
		$copypath =  'userimgs/'.$gid;
		//TODO:这里压缩打文件会高CPU而且会死的情况，如果太大则手动压缩
		$z -> Zip($copypath, 'userimgs/'.$gid.'.zip');
    $info.="      zip the document ok!";
		$this->ajaxReturn($info);
	 } else {
		$this->ajaxReturn('no goods is selected');
	 }
	}

	//list user 
 function userlists() {
	 
	 $this->assign('ulist',$this->getuserlist());
	 //$this->trace($ulist);
	$this->display();
 }

 function editperson() {
		$id = intval($_GET['gid']);
		//dump($id);
		$guest = D('ybkguest');
		$pinfo = $guest->where('`gid`='.$id)->select();
		$this->assign('info',$pinfo[0]);
		$this->display();
 }

 function savepinfo() {
	   //save person
		$guest = D('ybkguest');
		$guest->create();
		$result = $guest->save();
		if($result)
	 {
			$this->redirect('userlists');
	 }

 }

 private function getuserlist() {
		$u=D('ybkguest');
	  return $u->where('`genable`=1')->select();
 }
	//list user goods
 function showgoods() {
	 $id=intval($_GET['gid']);
	 //
		$p = M('ybkguest');
		$personinfo = $p->where('`gid`='.$id)->select();
		//	dump($personinfo);
	 //
	 $result = $this->returnugid($id);
	 $this->assign('goodsresult',$result);
	 $this->assign('count',count($result));
	 $this->assign('personinfo',$personinfo);
	 //dump($result);
	 $this->display();
 }

 function addperson()
 {
  $this->display();
 }

 function addpersonsave()
 {
  $info = "";
  $d = D('ybkguest');
  $validate = array(
   array('gname', '', 'account is exist!', 0, 'unique', 1), 
   // array('','')
   );
  $auto = array(
   array('password', 'md5', 1, 'function'),
   array('gaddtime', 'time', 1, 'function'),
   );
  $d->setProperty("_validate", $validate);
  $d->setProperty("_auto", $auto);
  $a = $d->create();
  if ($a)
  {
		$uid =$d->add();
   if ($uid)
   {
    $info = $uid."add ok";
   }
   else
   {
    $info = 'system error';
   }
  }
  else
  {
   $info = $d->getError();
  }
  $this->ajaxReturn($info);
 }
}



?>